package com.xuyiwei.mayi.test;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by xuyiwei on 2019/4/13.
 */
class TicketThreadRunnable implements Runnable{

    int count;

    private ReentrantLock lock = new ReentrantLock();

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public void run() {
//        synchronized (this){
             while (count>0){
                 lock.lock();
                 if(count>0) {
                     System.out.println(Thread.currentThread().getName() + "卖出一张票还剩:" + count + "张");
                     count--;
                 }
                 lock.unlock();

             }
 //        }
    }
}
public class TicketThread {
    public static void main(String[] args) {
        TicketThreadRunnable ticketThreadRunnable = new TicketThreadRunnable();
        ticketThreadRunnable.setCount(100);
        Thread t1 = new Thread(ticketThreadRunnable,"1号窗口");
        Thread t2 = new Thread(ticketThreadRunnable,"2号窗口");
        t1.start();t2.start();
    }
}
